<template>
  <el-dialog :title="title" :visible.sync="visibles">
    <el-form :model="form" :rules="rules">
      <el-form-item label="角色名称" prop="name" :label-width="formLabelWidth">
        <el-input v-model="form.name" autocomplete="off" />
      </el-form-item>
      <el-form-item
        label="角色描述"
        prop="description"
        :label-width="formLabelWidth"
      >
        <el-input v-model="form.description" autocomplete="off" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="deleteBtns">取 消</el-button>
      <el-button type="primary" @click="tureBtn">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addRole, editRoleById } from '../../../api/setting.js'
export default {
  name: 'SeettingDialog',
  data() {
    return {
      form: {
        name: '',
        description: ''
      },
      visibles: false, // 是否显示弹框
      formLabelWidth: '120px', // 左侧宽度
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', tragger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空', tragger: 'blur' }
        ]
      }
    }
  },
  computed: {
    title() {
      return this.form.id ? '编辑角色' : '新增角色'
    }
  },
  methods: {
    //   取消按钮
    deleteBtns() {
      this.form = {
        name: '',
        description: ''
      }
    },
    async tureBtn() {
      // 判断是编辑还是添加
      if (this.form.id) {
        // 编辑
        const edit = await editRoleById(this.form)
        this.$message.success('编辑成功')
      } else {
        // 添加
        const res = await addRole({
          name: this.form.name,
          description: this.form.description
        })
        this.$message.success('添加成功')
        console.log('res::', res)
      }
      //   关闭弹框
      this.visibles = false
      // 刷新页面
      this.$parent.getData()
      this.form = {
        name: '',
        description: ''
      }
    }
  }
}
</script>

<style></style>
